var form;
var layer;
//页面初始化时加载Layui框架
layui.use(['form', 'layer', 'laydate'], function () {
    form = layui.form();
    layer = layui.layer;
    $.removeTableCookie('SpryMedia_DataTables_noticeTable_noticeList.jsp');
    getDataTable();

    //新增
    $("#addNotice").click(function () {
        saveOrUpdateNotice(null);
    });
    // 查询
    $("#search").click(function () {
        $.removeTableCookie('SpryMedia_DataTables_noticeTable_noticeList.jsp');
        getDataTable(); //要触发的方法
        return false;
    });

    //回车事件
    $(document).keydown(function (event) {
        var code = event.which;
        if (code == 13) {
            $.removeTableCookie('SpryMedia_DataTables_noticeTable_noticeList.jsp');
            getDataTable(); //要触发的方法
            return false;
        }
    });
});
/**
 * 获取列表
 */
function getDataTable() {
    $('#noticeTable').dataTable({
        'bProcessing': true,
        'bServerSide': true,
        'fnServerParams': function (aoData) {
            var title = $.trim($("#title").val());
            var top = $("#top").val();
            var startTime = $.trim($("#startTime").val());
            var endTime = $.trim($("#endTime").val());
            aoData.push({
                "name": "notice.title",
                "value": title
            }, {
                "name": "notice.top",
                "value": top
            }, {
                "name": "notice.startTime",
                "value": startTime
            }, {
                "name": "notice.endTime",
                "value": endTime
            });
        },
        "sAjaxSource": basePath + "notice/getNoticeList.action", // 获取列表
        "sServerMethod": "POST",
        "sPaginationType": "full_numbers",
        "bPaginate": true, // 翻页功能
        "bStateSave": true, // 状态保存
        "bLengthChange": false, // 改变每页显示数据数量
        "bFilter": false, // 过滤功能
        "bSort": false, // 排序功能
        "bInfo": true, // 页脚信息
        "bAutoWidth": false, // 自动宽度
        "bDestroy": true,
        "iDisplayLength": 15, // 每页显示多少行
        "aoColumns": [{
            "sTitle": '排序',
            "mDataProp": "no"
        }, {
            "sTitle": '标题',
            "mDataProp": "title",
            "sClass": "conceal"
        }, {
            "sTitle": '内容',
            "mDataProp": "content",
            "sClass": "conceal"
        }, {
            "sTitle": '发布人',
            "mDataProp": "release"
        }, {
            "sTitle": '发布时间',
            "mDataProp": "releaseTime"
        }, {
            "sTitle": '逾期时间',
            "mDataProp": "overdueTime"
        }, {
            "sTitle": '操作',
            "mDataProp": null
        }],
        "oLanguage": {
            "sUrl": basePath + "plugins/datatable/cn.txt" // 中文包
        },
        "fnDrawCallback": function (oSettings) {
            $('#noticeTable tbody  tr td').each(function () {
                this.setAttribute('title', $(this).text());
            });
        },
        "fnInitComplete": function () {
        },
        "aoColumnDefs": [{
            "aTargets": [1],
            "fnRender": function (oObj) {
                var noticeId = oObj.aData.id;
                var top = oObj.aData.top;
                var title = oObj.aData.title;
                if (top == 1) {
                    return '<a href="javascript:void(0);" onclick=detailNotice("' + noticeId + '")><span style="color: #990000">[置顶]</span>' + title + '</a>';
                } else {
                    return '<a href="javascript:void(0);" onclick=detailNotice("' + noticeId + '")>' + title + '</a>';
                }
            }
        }, {
            "aTargets": [6],
            "fnRender": function (oObj) {
                var noticeId = oObj.aData.id;
                var top = oObj.aData.top;
                var isRelease = oObj.aData.isRelease;//是否已发布
                var overdue = oObj.aData.overdue;//是否逾期
                var html = '';
                if (overdue != 1) {
                    if (top == 1) {
                        html += '<a data-method="top" class="layui-btn layui-btn-small layui-btn-cancel layui-btn-radius" onclick=topNotice(\'' + noticeId + '\',\'' + top + '\')>取消</a>';
                    } else {
                        html += '<a data-method="top" class="layui-btn layui-btn-small layui-btn-top layui-btn-radius" onclick=topNotice(\'' + noticeId + '\',\'' + top + '\')>置顶</a>';
                    }
                    if(isRelease==1){
                        html += '<a data-method="repair" class="layui-btn layui-btn-small layui-btn-radius layui-btn-disabled" >修改</a>'
                            + '<a data-method="del" class="layui-btn layui-btn-small layui-btn-radius layui-btn-disabled" >删除</a>';
                    }else{
                        html += '<a data-method="repair" class="layui-btn layui-btn-small layui-btn-normal layui-btn-radius" onclick=saveOrUpdateNotice(\'' + noticeId + '\')>修改</a>'
                            + '<a data-method="del" class="layui-btn layui-btn-small layui-btn-danger layui-btn-radius" onclick=deleteNotice(\'' + noticeId + '\')>删除</a>';
                    }
                } else {
                    html = '<a data-method="repair" class="layui-btn layui-btn-small layui-btn-radius layui-btn-disabled" >修改</a>'
                        + '<a data-method="del" class="layui-btn layui-btn-small layui-btn-radius layui-btn-disabled" >删除</a>';
                }
                return html;
            }
        }]
    });
}

/**
 * 新增/修改
 */
function saveOrUpdateNotice(noticeId) {
    var url;
    var titleVal;
    if (null == noticeId) {
        url = "jsp/notice/addOrUpdateNotice.jsp";
        titleVal = "新增公告通知";
    } else {
        url = "notice/toUpdateOrDetail.action?type=update&id=" + noticeId;
        titleVal = "修改公告通知";
    }
    layer.open({
        type: 2,
        title: titleVal,
        area: ["50%", "45%"],
        shade: 0.3,
        maxmin: true,
        content: basePath + url,
        zIndex: layer.zIndex,
        success: function (layero) {//弹出后回调
            layer.setTop(layero); //弹窗置于最上层
        }
    });
}
/**
 * 置顶或取消置顶
 * @param noticeId
 */
function topNotice(noticeId, top) {
    if (top == 1) {
        layer.confirm('确定取消置顶此条公告通知?', {
            title: " 确认",
            icon: 3,
            skin: 'layui-layer-molv',
            btn: ["确定", "取消"],
            yes: function (index) {
                operation(noticeId);
                layer.close(index);
            }
        });
    } else {
        operation(noticeId);
    }
}
/**
 * 具体置顶或取消操作
 * @param noticeId
 */
function operation(noticeId){
    $.ajax({
        url: basePath + "notice/topNotice.action?id=" + noticeId,
        type: "post",
        dataType: 'text',
        success: function (data) {
            if (data != null && data != "" && data == 0) {
                getDataTable();
            } else {
                layer.open({
                    title: '错误',
                    icon: 2,
                    content: '操作失败！'
                });
                getDataTable();
            }
        }
    });
}

/**
 * 公告通知详情
 * @param noticeId
 */
function detailNotice(noticeId) {
    var url = "notice/toUpdateOrDetail.action?type=detail&id=" + noticeId;
    layer.open({
        type: 2,
        title: '查看公告通知',
        area: ["50%", "50%"],
        shade: 0.3,
        maxmin: true,
        content: basePath + url,
        btn: ['关闭'],
        yes: function (index) {
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },
        zIndex: layer.zIndex,
        success: function (layero) {
            layer.setTop(layero); //弹窗置于最上层
        }
    });
}

/**
 * 删除
 */
function deleteNotice(noticeId) {
    layer.confirm('确定删除此条数据?', {
        title: " 确认",
        icon: 3,
        skin: 'layui-layer-molv',
        btn: ["确定", "取消"],
        yes: function (index) {
            $.ajax({
                url: basePath + "notice/deleteNotice.action?id=" + noticeId,
                type: "post",
                dataType: 'text',
                success: function (data) {
                    if (data != null && data != "" && data == 0) {
                        getDataTable();
                    } else {
                        layer.open({
                            title: '错误',
                            icon: 2,
                            content: '删除失败！'
                        });
                        getDataTable();
                    }
                }
            });
            layer.close(index);
        }
    });
}